package com.inossem.warehousestorage_inossem.adapter.query;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.query.QueryOutStorageDetailActivity;
import com.inossem.warehousestorage_inossem.activity.query.QueryOutStorageListActivity;
import com.inossem.warehousestorage_inossem.activity.query.extra.QueryExtra;
import com.inossem.warehousestorage_inossem.bean.query.QueryOutStorageListBean;
import com.inossem.warehousestorage_inossem.utils.QueryUtils;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class QueryOutStorageListAdapter extends BaseAdapter {

    private Context mContext;
    private List<QueryOutStorageListBean.DataBean.OutStorageListBean> mOutStorageList;
    private LayoutInflater inflater;
    private QueryOutStorageListActivity.QueryOutStorageListListener mQueryOutStorageListListener;
    private String mOutStorageNumber;

    public QueryOutStorageListAdapter(Context context, List<QueryOutStorageListBean.DataBean.OutStorageListBean> outStorageList, String outStorageNumber, QueryOutStorageListActivity.QueryOutStorageListListener queryOutStorageListListener) {
        mContext = context;
        mOutStorageList = outStorageList;
        mQueryOutStorageListListener = queryOutStorageListListener;
        inflater = LayoutInflater.from(context);
        mOutStorageNumber = outStorageNumber;
    }

    @Override
    public int getCount() {
        return mOutStorageList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mOutStorageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_receive_list_first_item, null);
            EditText outStorageNumber = (EditText) convertView.findViewById(R.id.inStorageNumber);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            outStorageNumber.setText(mOutStorageNumber == null ? "" : mOutStorageNumber);
            outStorageNumber.setHint("请输入出库单号");
//            inStorageNumber.addTextChangedListener(new QRCodeEditTextWatcher(inStorageNumber) {
//                @Override
//                public void onScan(String text) {
//                    Utils.closeSoftKeyboard(mContext, inStorageNumber);
//                    mInStorageReceiveListener.onNoDoubleClickListener(text);
//                }
//            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mQueryOutStorageListListener.onNoDoubleClickListener(outStorageNumber.getText().toString());
                }
            });
        } else {
            QueryOutStorageListBean.DataBean.OutStorageListBean receiveInstorageItem = mOutStorageList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_query_out_storage_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, receiveInstorageItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.outStorageNumber = (TextView) view.findViewById(R.id.outStorageNumber);
        viewHolder.outStorageDate = (TextView) view.findViewById(R.id.outStorageDate);
        viewHolder.positionNumber = (TextView) view.findViewById(R.id.positionNumber);
        viewHolder.outStorageType = (TextView) view.findViewById(R.id.outStorageType);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, QueryOutStorageListBean.DataBean.OutStorageListBean outStorageItem) {

        viewHolder.outStorageNumber.setText(QueryUtils.changeFontColor(context, context.getResources().getString(R.string.view_query_out_storage_list_out_storage_number), outStorageItem.getOutStorageNumber(), R.color.common_font_item_name));
        viewHolder.outStorageDate.setText(outStorageItem.getOutStorageDate());
        viewHolder.positionNumber.setText(QueryUtils.changeFontColor(context, context.getResources().getString(R.string.view_query_out_storage_list_position_number), outStorageItem.getPositionNumber(), R.color.common_font_item_name));
        viewHolder.outStorageType.setText(outStorageItem.getOutStorageType());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, QueryOutStorageDetailActivity.class);
                intent.putExtra(QueryExtra.OUT_STORAGE_NUMBER, outStorageItem.getOutStorageNumber());
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView outStorageNumber, outStorageDate, positionNumber, outStorageType;
    }

}
